/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import controller.Action;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.IIOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Login;
import persistence.LoginDAO;

/**
 *
 * @author Cristian
 */
public class ConfirmarLoginAction implements Action {

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response, Integer contaLogada) throws IIOException {

        String numero = contaLogada.toString();
        String senha = request.getParameter("senha");

        if (numero.equals("") || senha.equals("")) {
            try {
                response.sendRedirect("indexErro.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(ConfirmarLoginAction.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                Login login = new Login();
                login.setNumero(Integer.parseInt(numero));
                login.setSenha(Integer.parseInt(senha));
                boolean confirmado = LoginDAO.getInstace().confirmaLogin(login);
                
                if(confirmado){
                    request.setAttribute("numeroConta", login.getNumero());
                    RequestDispatcher dispatcher = request.getRequestDispatcher("paginaInicial.jsp");
                    dispatcher.forward(request, response);
                   // response.sendRedirect("paginaInicial.xhtml");
                }else{
                    response.sendRedirect("indexErro.xhtml");
                }
            } catch (Exception e) {
                try {
                    response.sendRedirect("indexErro.xhtml");
                } catch (IOException ex) {
                    Logger.getLogger(ConfirmarLoginAction.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }
    }
}
